package com.hunanwenli.tool;
import com.hunanwenli.entity.Content;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.*;

/**
 * @author wanzhenyu
 * @date 2021/8/12 19:21
 */
public class HtmlParseUtil {

    private HtmlParseUtil() {
    }

    public static List<Content> getGoodsList(String url)  {
        Document document = getDocument(url);
        Element element = document.getElementById("J_goodsList");
        Elements li = element.getElementsByTag("li");
        List<Content> list = new ArrayList<>();
        for (Element el:li) {
                String id = el.getElementsByTag("li").attr("data-sku");
                String idHtml = "https://item.jd.com/"+id+".html";
                String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
                String price = el.getElementsByClass("p-price").eq(0).text();
                String name = el.getElementsByClass("p-name").eq(0).text();
                Content content = new Content();
                content.setPImg(img).setPName(name).setPPrice(price).setId(idHtml);
                list.add(content);
            }

        return list;
    }


    public static List<Content> groupShop(){
        Document document = getDocument("http://t.dianping.com/changsha");
        Elements elements = document.getElementsByClass("tg-floor-item-wrap");
        List<Content> list = new ArrayList<>(16);
        for (Element element: elements) {
            String href = element.getElementsByTag("a").attr("href");
            String id = "http://t.dianping.com"+href;
            String img = element.getElementsByTag("img").attr("lazy-src-load");
            String name = element.getElementsByTag("h3").text();
            String info = element.getElementsByTag("h4").text();
            String price = element.getElementsByTag("em").text();
            String oldPrice = element.getElementsByTag("del").text();
            String sold = element.getElementsByClass("tg-floor-sold").eq(0).text();
            Content content = new Content();
            content.setId(id).setPPrice(price).setOldPrice(oldPrice).setPName(name).setInfo(info).setPImg(img).setSold(sold);
            list.add(content);
        }
        return list;
    }
    public static List<Content> fastShop(){
        Document document = getDocument("https://www.55shangou.com/");
        Elements elements = document.getElementsByClass("hot");
        List<Content> list = new ArrayList<>(16);
        for (Element element:elements) {
            String href = element.getElementsByTag("a").attr("href");
            String img = element.getElementsByTag("img").attr("src");
            String name = element.getElementsByClass("hot-title").eq(0).text();
            String price = element.getElementsByClass("hot-price").eq(0).text();
            Content content = new Content();
            content.setId(href).setPName(name).setPImg(img).setPPrice(price);
            list.add(content);
        }
        return list;
    }

    private static Document getDocument(String url){
        Random r = new Random();
        String[] ua = {"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36 OPR/37.0.2178.32",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
                "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
                "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
                "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
                "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0)",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 BIDUBrowser/8.3 Safari/537.36",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.12150.8 Safari/537.36",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 TheWorld 7",
                "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/60.0"};
        int j = r.nextInt(14);
        Map<String, String> map2 = new HashMap<String, String>(16);
        map2.put("waybillNo", "DD1838768852");
        Document document = null;
        try {
            Connection conn = Jsoup.connect(url)
                    .timeout(5000)
                    //.proxy(ip, port)
                    .data(map2)
                    .ignoreContentType(true)
                    .userAgent(ua[j]);
            conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            conn.header("Accept-Encoding", "gzip, deflate, sdch");
            conn.header("Accept-Language", "zh-CN,zh;q=0.8");
            conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
            document = conn.get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }
}
